from src.common.utils.websocket.WebSocketUtil import WebSocketUtil
from resources.config import config

wsUtil = WebSocketUtil()

def onOpen(ws):
    print(f"连接成功")

def onMessage(ws, msg):
    print(f"接收到消息：{msg}")

def onError(ws, error):
    print(f"发生错误：{error}")

def onClose(ws, code, msg):
    print(f"断开连接:{code}-{msg}")

def getProtocol():
    msg = '{"protocolId": 10001}'
    wsUtil.ws.send(msg)

def setProtocol():
    msg = f'{{"protocolId": 10021, "data": {{"key": "motorState", "value": {input("请设置灯的状态")}}}}}'
    wsUtil.ws.send(msg)


if __name__ == '__main__':
    wsUtil.onOpen = onOpen
    wsUtil.onMessage = onMessage
    wsUtil.onError = onError
    wsUtil.onClose = onClose
    wsUtil.connect(config.server.websocketUrl+"user:1")
    while True:
        ctrl = input("请选择命令：")
        if ctrl=="g":
            getProtocol()
        elif ctrl=="s":
            setProtocol()
        elif ctrl=="q":
            import os
            os._exit(0)
